﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Areas.PersonalAccount.Controllers
{
    public class PersonalAccount_AlterEmailAddressController : Controller
    {
        public IActionResult Create(string taskId)
        {
            PersonalAccount_AlterEmailAddress model = new PersonalAccount_AlterEmailAddress();
            ViewBag.TaskId = taskId;
            ViewBag.IsCheckman = false;
            ViewBag.Resubmit = false;
            ViewBag.ActionList = new List<WorkflowTransition>();
            if (!string.IsNullOrEmpty(taskId))
            {
                Expression<Func<V_WorkflowTask, bool>> exp = workflowTask => workflowTask.TaskId == taskId;
                List<V_WorkflowTask> workflowTaskList = new V_WorkflowTaskBusiness().GetList(exp).ToList();
                if (workflowTaskList != null && workflowTaskList.Count > 0)
                {
                    PersonalAccount_AlterEmailAddress tempModel = new PersonalAccount_AlterEmailAddressBusiness().Get(workflowTaskList[0].WorkflowTypeId);
                    if (tempModel != null) model = tempModel;
                    (bool isCheckman, bool resubmit, List<WorkflowTransition> actionList) = WorkflowCommonBusiness.GetWorkflowInfo(taskId, SessionUtility.GetBasicUserInfo().PinyinCode);
                    ViewBag.IsCheckman = isCheckman;
                    ViewBag.Resubmit = resubmit;
                    ViewBag.ActionList = actionList;
                }
            }
            return View(model);
        }

        [HttpPost]
        public IActionResult Submit(PersonalAccount_AlterEmailAddress alterEmailAddress, string action, string taskId = null)
        {
            try
            {
                #region 完善PersonalAccount_AlterEmailAddress
                if (string.IsNullOrEmpty(alterEmailAddress.Id)) { alterEmailAddress.Id = Guid.NewGuid().ToString(); }
                alterEmailAddress.Creator = SessionUtility.GetBasicUserInfo().PinyinCode;
                alterEmailAddress.CreateDate = DateTime.Now;
                #endregion
                (bool success, string message) = new PersonalAccount_AlterEmailAddressBusiness().Submit(alterEmailAddress, SessionUtility.GetBasicUserInfo().PinyinCode, action, taskId);
                return Json(new { Success = success, Message = message });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "邮件地址变更提交失败", $"alterEmailAddress={JsonConvert.SerializeObject(alterEmailAddress)}，action={action}，taskId={taskId}", ex, "DapperCore");
                return Json(new { Success = false, Message = AlertMessage.SubmitException });
            }
        }

        public IActionResult Audit(string taskId)
        {
            PersonalAccount_AlterEmailAddress model = new PersonalAccount_AlterEmailAddress();
            ViewBag.TaskId = taskId;
            ViewBag.IsCheckman = false;
            ViewBag.ActionList = new List<WorkflowTransition>();
            ViewBag.Dept1Name = "";
            ViewBag.Dept2Name = "";
            ViewBag.Dept3Name = "";
            if (!string.IsNullOrEmpty(taskId))
            {
                Expression<Func<V_WorkflowTask, bool>> exp = workflowTask => workflowTask.TaskId == taskId;
                List<V_WorkflowTask> workflowTaskList = new V_WorkflowTaskBusiness().GetList(exp).ToList();
                if (workflowTaskList != null && workflowTaskList.Count > 0)
                {
                    PersonalAccount_AlterEmailAddress tempModel = new PersonalAccount_AlterEmailAddressBusiness().Get(workflowTaskList[0].WorkflowTypeId);
                    if (tempModel != null) model = tempModel;
                    (bool isCheckman, bool resubmit, List<WorkflowTransition> actionList) = WorkflowCommonBusiness.GetWorkflowInfo(taskId, SessionUtility.GetBasicUserInfo().PinyinCode);
                    ViewBag.IsCheckman = isCheckman;
                    ViewBag.ActionList = actionList;
                }
            }
            if (!string.IsNullOrEmpty(model.DeptId))
            {
                Sys_Department department = new Sys_DepartmentBusiness().Get(model.DeptId);
                ViewBag.Dept1Name = department.Dept1Name;
                ViewBag.Dept2Name = department.Dept2Name;
                ViewBag.Dept3Name = department.Dept3Name;
            }
            return View(model);
        }

        [HttpPost]
        public IActionResult Audit(string taskId, string action, string remarks)
        {
            try
            {
                (bool success, string message) = new PersonalAccount_AlterEmailAddressBusiness().Audit(taskId, SessionUtility.GetBasicUserInfo().PinyinCode, action, remarks);
                return Json(new { Success = success, Message = message });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "邮件地址变更审批失败", "taskId=" + taskId + "，remarks=" + remarks + "，action=" + action, ex, "DapperCore");
                return Json(new { Success = false, Message = AlertMessage.AuditException });
            }
        }
    }
}